package com.link.base.base.project.service;

import com.link.base.base.project.dao.mybatis.mapper.BestSellingCarMapper;
import com.link.base.base.project.model.BestSellingCar;
import com.link.base.base.project.model.CarAnnualSales;
import com.link.base.core.basic.util.BigDecimalUtils;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * 前20畅销车型销售额
 *
 * @author 王昊
 * @date 2021-05-07 14:20:20
 */
@Service
public class BestSellingCarServiceImpl extends BasicServiceImpl<BestSellingCar> implements BestSellingCarService {

    @Resource
    private BestSellingCarMapper    bestSellingCarMapper;

    @Resource
    private CarAnnualSalesService carAnnualSalesService;

    @Override
    public BasicMapper<BestSellingCar> getBasicMapper() {
        return bestSellingCarMapper;
    }

    @Override
    public void beforUpsert(BestSellingCar entity) throws Exception {
        super.beforUpsert(entity);
        //销售额占比 = 量产销售额/一彬总量产销售额。一彬总量产销售额来源：头列表字段“一彬总量产销售额
        //根据头ID查询头，拿到总产量销售额
        CarAnnualSales querAnnualSales = new CarAnnualSales();
        querAnnualSales.setId(entity.getCarAnnualSalesId());
        CarAnnualSales carAnnualSales = carAnnualSalesService.queryById(querAnnualSales);
        if (carAnnualSales.getTotalOutputSales() == BigDecimal.ZERO || carAnnualSales.getTotalOutputSales() == null) {
            //本年度一彬总产量销售额输入值为空或为零，请重新输入！
            throw new ServiceException("IYUPROJECT-007");
        }
        BigDecimal totalOutputSales = carAnnualSales.getTotalOutputSales() == null ? new BigDecimal(1) : carAnnualSales.getTotalOutputSales();
        BigDecimal massProdSales = entity.getMassProdSales() == null ? BigDecimal.ZERO : entity.getMassProdSales();
        if (massProdSales.compareTo(totalOutputSales) == 1) {
            //分子大于分母  量产销售额大于一彬总产量销售额 "本行量产销售额大于本年度一彬总产量销售额，请重新输入！"
            throw new ServiceException("IYUPROJECT-008");
        }
        entity.setSalesPercent(massProdSales.divide(totalOutputSales,4,BigDecimal.ROUND_HALF_UP));
    }

    /**
     * 前20畅销车型销售额分析（饼图）
     */
    @Override
    public List<BestSellingCar> queryAllByYear(BestSellingCar entity) throws Exception {
        //计算【其他】模块的畅销车型，量产销售额，利润率，销售额占比
        List<BestSellingCar> bestSellingCars = bestSellingCarMapper.queryAllByYear(entity);
        if (bestSellingCars != null && !bestSellingCars.isEmpty()) {
            BestSellingCar getTotalOutputSales = bestSellingCars.get(0);
            BigDecimal totalOutputSales = BigDecimalUtils.changeNullToZero(getTotalOutputSales.getTotalOutputSales());
            BigDecimal sumMassProdSales = new BigDecimal(BigInteger.ZERO);
            for (BestSellingCar bestSellingCar : bestSellingCars) {
                BigDecimal MassProdSales = BigDecimalUtils.changeNullToZero(bestSellingCar.getMassProdSales());
                sumMassProdSales = sumMassProdSales.add(MassProdSales);
            }
            BigDecimal value = totalOutputSales.subtract(sumMassProdSales);
            if (value.compareTo(BigDecimal.ZERO) == -1) {
                throw new ServiceException("IYUPROJECT-009");
            }
            BestSellingCar other = new BestSellingCar();
            other.setMassProdSales(value);
            other.setBestSellModel("其他");
            other.setProfitMargin(null);
            if (totalOutputSales != BigDecimal.ZERO) {
                other.setSalesPercent(value.divide(totalOutputSales,4,BigDecimal.ROUND_HALF_UP));
            }
            bestSellingCars.add(other);
        }
        return bestSellingCars;
    }
}